﻿using Authorization.Center.Controllers;
using Authorization.Center.Dto;
using Authorization.Center.SqlServer;
using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using S.Common.helper;
using S.EliminationOrDath.API.Application.Command.EOrDCommand;
using S.EliminationOrDath.API.Dtos;
using S.EliminationOrDath.Domain.Models;
using static S.EliminationOrDath.API.Application.Command.EOrDCommand.EliminationCommand;

namespace S.EliminationOrDath.API.Controllers
{
    /// <summary>
    /// 控制器写--淘汰
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class EliminationController : BaseController
    {
        private readonly IMediator mediator;
        private readonly IConfiguration configuration;

        /// <summary>
        /// 构造函数，用于依赖注入
        /// </summary>
        /// <param name="mediator">中介者实例</param>
        public EliminationController(IMediator mediator, IConfiguration configuration, MySqlDbContext db) : base(db)
        {
            this.mediator = mediator;
            this.configuration = configuration;
        }
        /// <summary>
        /// 用户编号
        /// </summary>
        public int UId { get { return UserStrategyDto.UserId; } }
        /// <summary>
        /// 用户名称
        /// </summary>
        public string? UName { get { return UserStrategyDto.UserTrueName; } }

        #region 添加

        /// <summary>
        /// 批量添加淘汰表数据
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> AddsElimination(List<EliminationDtos?>? models)
        {
            foreach (var item in models)
            {
                item.CreateBy = UName;
            }
            return Ok(await mediator.Send(new AddsEliminationCommand() { eliminations = models }));
        }
        /// <summary>
        /// 测试授权
        /// </summary>
        /// <returns></returns>
        [Authorize]
        [HttpGet]
        public IActionResult sp()
        {
            return Ok(UId);
        }

        #endregion

        #region 修改

        /// <summary>
        /// 批量审批
        /// </summary>
        /// <param name="dtos"></param>
        /// <returns></returns>
        [Authorize]
        [HttpPut]
        public async Task<IActionResult> ExamineStatus(List<EliminationDtos> dtos,bool onshelf)
        {
            return Ok(await mediator.Send(new ExamineStatusCommand() { eliminationDtos = dtos,onShelf = onshelf,UserId = UId,UserName = UName}));
        }
        /// <summary>
        /// 修改淘汰表信息
        /// </summary>
        /// <param name="dtos"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task<IActionResult> UpdElimination(EliminationDtos dtos)
        {
            dtos.UpdateBy = UName;
            dtos.UpdateDate = DateTime.Now;
            return Ok(await mediator.Send(new UpdEliminationCommand() { eliminationDtos = dtos}));
        }
        #endregion

        #region 删除

        /// <summary>
        /// 批量删除淘汰数据
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [HttpDelete]
        public async Task<IActionResult> DelEli(string ids)
        {
            return Ok(await mediator.Send(new DelEliminationCommand() { ids = ids}));
        }

        #endregion

        [HttpGet]
        [Route("TestConsul")]
        public string TestConsul()
        {
            string result = $"您当前正在访问{configuration["Service:Port"].ToString()}端口服务！";
            return result;
        }
    }
}
